package com.ald.crossex.service.wechat;

import com.ald.crossex.dao.CeCoreUserDao;
import com.ald.crossex.dao.CeGoodsDao;
import com.ald.crossex.dao.CeGoodsTypeDao;
import com.ald.crossex.dto.wechat.BaseResponse;
import com.ald.crossex.dto.wechat.PublishDeliveryRequest;
import com.ald.crossex.entity.TCeCoreUser;
import com.ald.crossex.entity.TCeGoods;
import com.ald.crossex.util.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by lishijiong on 15/3/8.
 */
@Service
public class DeliveryService {

    @Autowired
    private CeGoodsTypeDao goodsTypeDao;
    @Autowired
    private CeGoodsDao goodsDao;
    @Autowired
    private CeCoreUserDao userDao;

    /**
     * 发布速递信息
     * @param request
     */
    @Transactional
    public BaseResponse publishGoods(PublishDeliveryRequest request){

        List<TCeCoreUser> users = userDao.queryByHql("from TCeCoreUser where uid = ?",new Object[]{request.getUid()});

        if(users.size()<0){
            return new BaseResponse(ErrorCode.OPTION_ERROR.getCode(),"不存在用户uid="+request.getUid()+",发布行程失败！");
        }

        TCeCoreUser user = users.get(0);
        TCeGoods goods = new TCeGoods();
        goods.setUid(user.getUid());
        goods.setName(request.getName());
        //物品类型
        goods.setTid(request.getType());
        //发收地点
        goods.setStart(request.getSendPlace());
        goods.setEnd(request.getReceivePlace());
        //重量
        goods.setWeight(request.getWeight());
        goods.setPrice(request.getPrice());
        //配送费用
        goods.setDeliveryPrice(request.getDeliveryPrice());
        goods.setDeliveryTime(request.getDeliveryTime());
        //发货时间
        goods.setPickupTime(request.getPickUpDateTime());
        goods.setSender(request.getSender());
        goods.setSenderPhone(request.getSendPhone());
        goods.setReceiver(request.getReceiver());
        goods.setReceiverPhone(request.getReceivePhone());
        goods.setRemark(request.getRemark());
        goods.setIdCard(request.getIdCard());
        //发货类型
        int type = 0;
        if(request.getAddPrice()>0){
         type = 3;
        }
        goods.setType(type);

        goods.setCreateTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setCityId(1);
        goods.setIsDelete(0);

        goodsDao.create(goods);

       return new BaseResponse(ErrorCode.OPTION_SUCCESS.getCode(),"发布成功，尽快为您安排。");


    }


}
